import React from 'react'

import apiAgentType from '../../../api/agentType';
import { Form, Input, Tooltip, Icon, Cascader, Select, Row, Col, Checkbox, Button, Message ,TreeSelect } from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;

import './index.less'

class AgentTypeForm extends React.Component {
  state = {
    confirmDirty: false,
  };
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {   //提交后得到from表单中得值
      if (!err) {
        if (values.id) {
          this.update(values);
        } else {
          this.add(values);
        }
      }
    });
  }

  add = (values) => {
    apiAgentType.post('/addAgentType', values).then((data) => {
      const { close ,refeshData} = this.props;
      const result = data.data;
      if (result.code == 0) {
        Message.success('添加成功！');
        close();
        refeshData();
      } else {
        Message.error('添加失败！');
      }
    });
  }
  update = (values) => {
    apiAgentType.put('/updateAgentType', values).then((data) => {
      const result = data.data;
      const { close ,refeshData} = this.props;
      if (result.code == 0) {
        Message.success("修改成功");
        close();
        refeshData();
      } else {
        Message.error("修改失败");
      }
    });
  }
  render() {
    const { getFieldDecorator, resetFields } = this.props.form;
    const { close, callbackParent, rolesData,maxSort} = this.props;
    const agentType = this.props.rowRecord;
    // 把重置表单的方法传给父亲
    callbackParent({
      resetFields: resetFields
    });

    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };
    const tailFormItemLayout = {
      wrapperCol: {
        xs: {
          span: 14,
          offset: 0,
        },
        sm: {
          span: 14,
          offset: 6,
        },
      },
    };
    return (
      <Form onSubmit={this.handleSubmit}>
        <FormItem>
          {getFieldDecorator('id', {
            initialValue: agentType ? agentType.id : ''
          })(
            <Input type="hidden" />
            )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="名称"
          hasFeedback
        >
          {getFieldDecorator('name', {
            rules: [{
              required: true, message: '请输入你的名称!',
            }],
            initialValue: agentType ? agentType.name : ''
          })(
            <Input />
            )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="用户角色"
        >
          {getFieldDecorator('roleId', {
            rules: [{ required: true, message: '必须至少选择一个角色!' }],
            initialValue: agentType ? agentType.roleId : []
          })(
            <TreeSelect treeData={rolesData}
              searchPlaceholder={'请至少选择一个角色'} dropdownStyle={{ maxHeight: '240px' }}
            />
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="描述"
          hasFeedback
        >
          {getFieldDecorator('descr', {
            initialValue: agentType ? agentType.descr : ''
          })(
            <Input />
            )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="排序号"
          hasFeedback
        >
          {getFieldDecorator('sort', {
            rules: [{
              required: true, message: '请输入排序号!',
            }],
            initialValue: agentType ? agentType.sort : (maxSort?maxSort:10)
          })(
            <Input />
            )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="备注"
          hasFeedback
        >
          {getFieldDecorator('remarks', {
            initialValue: agentType ? agentType.remarks : ''
          })(
            <Input />
            )}
        </FormItem>
        <FormItem {...tailFormItemLayout}>
          <div>
            <Button type="primary" htmlType="submit" style={{ marginRight: '10px' }}>确定</Button>
            <Button onClick={close}>取消</Button>
          </div>
        </FormItem>
      </Form>
    );
  }
}

const WrappedAgentTypeForm = Form.create()(AgentTypeForm);
export default WrappedAgentTypeForm
